<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\goods\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Cache;
use think\Db;
use app\goods\model\Style as StyleModel;
use app\goods\model\AttrGroup as AttrGroupModel;
use app\goods\model\Attr as AttrModel;

/**
 * 商品类型
 */
class Style extends Admin
{
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $data_list = StyleModel::styleSelect('','styleid DESC');     
        return ZBuilder::make('table')
        ->addColumns([ // 批量添加列
            ['stylename', '名称'],
            ['attrgroupname', '属性组'],
            ['status', '状态' , 'status'],
            ['right_button', '操作', 'btn']
        ])
        ->setTableName('goods_style')
        ->setPrimaryKey('styleid')
        ->addRightButton('edit')
        ->addRightButton('delete')
        ->addTopButton('add')
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *新增
    */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if($data['hide'] == 1){
                if(empty($data['stylename']) || empty($data['attrgroupid'])){
                    $this->error('请完善基本信息');
                }

                return $this->attr($data['attrgroupid'], $data['stylename']);
            }else{
                if(!empty($data['attrid'])){
                    $add_data['attrid'] = ','.implode(',', $data['attrid']).',';
                }
                $add_data['stylename']   = $data['stylename'];
                $add_data['attrgroupid'] = $data['attrgroupid'];
                $add_data['status']      = $data['status'];
                $result = StyleModel::insert($add_data);
                if($result) {
                    $this->success('新增成功', cookie('__forward__'));
                }else{
                    $this->error('新增失败');
                }

            }
        }

        $attr_group = AttrGroupModel::where('status', '1')->column('attrgroupid,attrgroupname');
        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列            
            ['text', 'stylename', '名称', '<span style="color:red">必填</span>'],
            ['select', 'attrgroupid', '属性组', '<span style="color:red">必填</span>', $attr_group],
            ['hidden', 'hide', '1'],
            
        ])
        ->setPageTitle('【Step1】填写基本信息')
        ->layout(['stylename' => 6, 'attrgroupid' => 6])
        ->isAjax(false)
        ->fetch();
    }

    /**
    *修改
    */
    public function edit($id = 0){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if($data['hide'] == 1){
                if(empty($data['stylename']) || empty($data['attrgroupid'])){
                    $this->error('请完善基本信息');
                }
                return $this->attr($data['attrgroupid'], $data['stylename'], $data['styleid']);
            }else{
                if(!empty($data['attrid'])){
                    $add_data['attrid'] = ','.implode(',', $data['attrid']).',';
                }
                $add_data['stylename']   = $data['stylename'];
                $add_data['attrgroupid'] = $data['attrgroupid'];
                $add_data['status']      = $data['status'];
                $add_data['styleid']     = $data['styleid'];
                $result = StyleModel::update($add_data);
                if($result) {
                    $this->success('修改成功', cookie('__forward__'));
                }else{
                    $this->error('修改失败');
                }

            }
        }

        $js = <<<EOF
            <script type="text/javascript">
                $(function(){
                    var s = 1;
                    $('select').change(function(){
                        if(s == 1){
                        alert('改变属性组可能会丢失部分属性');
                        s = 2;
                    }

                    })
                });
            </script>
EOF;

        //详情
        $info = StyleModel::where('styleid', $id)->find();
        $attr_group = AttrGroupModel::where('status', '1')->column('attrgroupid,attrgroupname');
        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列            
            ['text', 'stylename', '名称', '<span style="color:red">必填</span>'],
            ['select', 'attrgroupid', '属性组', '<span style="color:red">必填</span>', $attr_group],
            ['hidden', 'hide', '1'],
            ['hidden', 'styleid'],
            
        ])
        ->setFormData($info)
        ->setPageTitle('【Step1】填写基本信息')
        ->layout(['stylename' => 6, 'attrgroupid' => 6])
        ->isAjax(false)
        ->setExtraJs($js)
        ->fetch();
    }


    /**
    *删除
    */
    public function delete($ids = 0){
        $id   = input('ids');
        $result = StyleModel::where(array('styleid' => $id))->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }


    /**
    *属性列表
    */
    public function attr($attrgroupid = 0, $stylename = '', $styleid = 0){

        //所有启用属性
        $attr_list = AttrModel::where(array('status' => '1', 'attrgroupid' => $attrgroupid))->select();

        //单选
        $radio        = [];
        $radioarr1    = [];
        $radioarr2    = [];
        //多选
        $checkbox     = [];
        $checkboxarr1 = [];
        $checkboxarr2 = [];
        //文本
        $text         = [];
        $textarr1     = [];
        $textarr2     = [];
        //下拉
        $select       = [];
        $selectarr1   = [];
        $selectarr2   = [];
        //图片
        $image        = [];
        $imagearr1    = [];
        $imagearr2    = [];
        foreach ($attr_list as $key => $value) {
            switch ($value['inittype']) {
                case '1':
                    $textarr1[] = $value['attrid'];
                    $textarr2[] = $value['attrname'];
                    break;
                case '2':
                    $imagearr1[] = $value['attrid'];
                    $imagearr2[] = $value['attrname'];
                    break;
                case '3':
                    $selectarr1[] = $value['attrid'];
                    $selectarr2[] = $value['attrname'];
                    break;
                case '4':
                    $radioarr1[] = $value['attrid'];
                    $radioarr2[] = $value['attrname'];
                    break;
                case '5':
                    $checkboxarr1[] = $value['attrid'];
                    $checkboxarr2[] = $value['attrname'];
                    break;
                
            }
        }
        $text     = array_combine($textarr1,$textarr2);
        $image    = array_combine($imagearr1,$imagearr2);
        $select   = array_combine($selectarr1,$selectarr2);
        $radio    = array_combine($radioarr1,$radioarr2);
        $checkbox = array_combine($checkboxarr1,$checkboxarr2);
        

        //详情
        $info = StyleModel::where('styleid',$styleid)->find();
        $info['attrgroupid'] = $attrgroupid;
        $info['stylename']   = $stylename;

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列
            ['hidden', 'styleid'],
            ['checkbox','attrid','文本录入','',$text],
            ['checkbox','attrid','图片录入','',$image], 
            ['checkbox','attrid','下拉录入','',$select], 
            ['checkbox','attrid','单选录入','',$radio], 
            ['checkbox','attrid','多选录入','',$checkbox],
            ['hidden', 'attrgroupid', $attrgroupid],
            ['hidden', 'stylename', $stylename],
            ['hidden', 'hide', 2],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],   
        ])
        ->setFormData($info)
        ->fetch();

    }


   

    
   
}
